<template>
  <div class="content">
    <div class="header">
      <h3 class="title">帖子列表</h3>
    </div>
    <div class="table">
      <el-table :data="postsList" border style="width: 100%">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60px"
        />

        <el-table-column
          align="center"
          label="标题"
          prop="title"
          width="200px"
        />

        <el-table-column
          align="center"
          label="发帖人"
          prop="name"
          width="200px"
        />

        <el-table-column align="center" label="帖子封面" prop="image_url">
          <template #default="scope">
            <img style="width: 50px" :src="scope.row.image_url" alt="" />
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="点赞数"
          prop="likeCount"
          width="100px"
        />
        <el-table-column
          align="center"
          label="收藏数"
          prop="collectionCount"
          width="100px"
        />
        <el-table-column align="center" label="创建时间" prop="created_at">
          <template #default="scope">
            {{ formatUTC(scope.row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="250px">
          <template #default="scope">
            <el-button
              size="small"
              icon="document"
              type="info"
              text
              @click="handleContentBtnClick(scope.row)"
            >
              内容详情
            </el-button>

            <el-button
              v-if="role === 2"
              size="small"
              icon="close"
              type="warning"
              text
              @click="handleRejectBtnClick(scope.row.id)"
            >
              退回
            </el-button>
            <el-button
              v-if="role === 2"
              size="small"
              icon="delete"
              type="danger"
              text
              @click="handleDeleteBtnClick(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="postsTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog v-model="dialogTableVisible" title="帖子内容" :width="900">
      <div v-html="contentDetail?.content"></div>
      <div>
        <CommentItem
          v-for="child in comment"
          :key="child.id"
          :comment="child"
          :allComments="comment"
          :role="role"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import type { IPost } from '@/store/main/post/type'
import usePostStore from '@/store/main/post/post'
import { formatUTC } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import CommentItem from '@/components/comment-item/comment-item.vue'
import { getRole } from '@/utils/handle-role'

// 1.发起action，请求usersList的数据
const postStore = usePostStore()
const currentPage = ref(1)
const pageSize = ref(10)
const role = ref(getRole())

fetchPostListData()

// 2.获取usersList数据,进行展示
const { postsList, postsTotalCount, comment } = storeToRefs(postStore)

// 3.页码相关的逻辑
function handleSizeChange() {
  fetchPostListData()
}
function handleCurrentChange() {
  fetchPostListData()
}

// 4.定义函数, 用于发送网络请求
function fetchPostListData(formData?: any) {
  // 1.获取offset/size
  const limit = pageSize.value
  const offset = (currentPage.value - 1) * limit
  // 2.发起网络请求
  if (formData) {
    postStore.getPostsAction({
      title: formData.title,
      name: formData.name,
      limit,
      offset,
      status: formData.status
    })
    return
  }
  postStore.getPostsAction({ limit, offset, status: 'approved' })
}

// 查看详细内容
const dialogTableVisible = ref(false)
const contentDetail = ref<IPost>()
function handleContentBtnClick(row: any) {
  contentDetail.value = row
  dialogTableVisible.value = true
  postStore.searchCommentByIdAction(row.id)
}

// 驳回帖子
async function handleRejectBtnClick(id: number) {
  ElMessageBox.confirm(`确认驳回这个帖子吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await postStore.updatePostStatusAction(id, 'rejected')
    ElMessage({
      type: 'success',
      message: '退回帖子成功',
      duration: 1000
    })
    fetchPostListData()
  })
}

// 删除数据
async function handleDeleteBtnClick(id: number) {
  ElMessageBox.confirm(`确认删除这个帖子吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await postStore.removePostAction(id)
    ElMessage({
      type: 'success',
      message: '删除帖子成功',
      duration: 1000
    })
    fetchPostListData()
  })
}

defineExpose({
  fetchPostListData
})
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
